<?php

namespace App\Filament\Resources;

use App\Filament\Resources\ActionResource\Pages;
use App\Models\Action;
use Filament\Forms;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Table;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Actions\CreateAction;

class ActionResource extends Resource
{
    protected static ?string $model = Action::class;

    protected static ?string $navigationIcon = 'heroicon-o-light-bulb';

    protected static ?int $navigationSort = 1;

    // 导航项分组翻译
    public static function getNavigationGroup(): ?string
    {
        return __('menu.action_group.name');
    }

    // 菜单标题导航翻译
    public static function getNavigationLabel(): string
    {
        return __('menu.action_group.action');
    }

    // 面包屑导航翻译
    public static function getModelLabel(): string
    {
        return __('menu.action_group.action');
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Textarea::make('content')->label(__('action.Content'))->rows(4),
            ])
            ->columns(1);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                ImageColumn::make('user.avatar')
                    ->label(__('action.User_avatar'))
                    ->circular()
                    ->size(60)
                    ->alignCenter(),
                TextColumn::make('user.name')
                    ->label(__('action.User_name'))
                    ->badge()
                    ->alignCenter(),
                TextColumn::make('content')->label(__('action.Content'))->copyable()->wrap(),
                TextColumn::make('created_at')->label(__('action.Created_at'))->width(50),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                CreateAction::make()
                    ->mutateFormDataUsing(function (array $data): array {
                        $data['user_id'] = auth()->id();
                        return $data;
                    })
            ])
            ->actions([
                EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ])
            ->defaultSort('id', 'desc');
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListActions::route('/'),
        ];
    }
}
